#ifndef PSFDetectorConstruction_h
#define PSFDetectorConstruction_h 1

#include "globals.hh"
#include "G4Cache.hh"
#include "G4RotationMatrix.hh"
#include "G4VUserDetectorConstruction.hh"
#include <CLHEP/Units/SystemOfUnits.h>

class PSFMaterials;
class PSFDetectorMessenger;
class PSFPhotonDetSD;

class G4Box;
class G4EllipticalTube;
class G4LogicalVolume;
class G4Material;
class G4Tubs;
class G4VisAttributes;
class G4VPhysicalVolume;

class PSFDetectorConstruction : public G4VUserDetectorConstruction
{
public:
  PSFDetectorConstruction();
  ~PSFDetectorConstruction() override;

  G4VPhysicalVolume *Construct() override;
  G4VPhysicalVolume *ConstructDetector();

  void ConstructSDandField() override;

  // Set Material Commands for World and PSFCore
  void SetWorldMaterial(G4String);
  void SetPSFCoreMaterial(G4String);
  void SetCoupleMaterial(G4String);

  void SetPhotonDetGeometry(G4String);
  void SetNumberOfCladding(G4int); // Maximum 2 claddings

  void SetPSFLength(G4double); // Total length of PSF
  void SetPSFCoreRadius(G4double);
  void SetClad1Radius(G4double);
  void SetClad2Radius(G4double);
  void SetPhotonDetHalfLength(G4double);
  void SetGap(G4double);
  void SetPhotonDetAlignment(G4double);
  // Set the ratio of x and y (x/y) radius of the ellipse
  void SetXYRatio(G4double);
  // Set the Roughness in between each layer
  void SetSurfaceRoughness(G4double);
  // Set the reflectivity of the mirror
  void SetMirrorReflectivity(G4double);
  // Set the polish of the mirror
  void SetMirrorPolish(G4double);
  // Set the reflectivity of the mirror
  void SetPhotonDetReflectivity(G4double);
  // Set the polish of the mirror
  void SetPhotonDetPolish(G4double);

  void SetMirror(G4bool);

  void SetBarLength(G4double);
  void SetBarBase(G4double);
  void SetHoleRadius(G4double);
  void SetCoatingThickness(G4double);
  void SetCoatingRadius(G4double);

  G4double GetPSFCoreLength();
  G4double GetPSFCoreEnd();
  G4double GetPSFRMax();
  G4double GetSurfaceRoughness();
  G4bool IsPerfectFiber();

  G4double GetBarLength();
  G4double GetBarBase();
  G4double GetHoleRadius();
  G4double GetHoleLength();
  G4double GetFiberRadius();

  G4double GetCoatingThickness();
  G4double GetCoatingRadius();

  G4Material *FindMaterial(G4String);
  //////////////////////////////////////////////
  G4double GetCladRadius() { return fClad1RX; };
  /////////////////////////////////////////////

private:
  std::vector<G4VisAttributes *> fVisAttributes;

  PSFMaterials *fMaterials = nullptr;

  G4LogicalVolume *fLogicHole = nullptr;
  G4LogicalVolume *fLogicWorld = nullptr;

  G4VPhysicalVolume *fPhysiWorld = nullptr;
  G4VPhysicalVolume *fPhysiHole = nullptr;

  G4double fWorldSizeX = -1.;
  G4double fWorldSizeY = -1.;
  G4double fWorldSizeZ = -1.;

  G4double fPSFCoreRX = -1.;
  G4double fPSFCoreRY = 0.5 * CLHEP::mm;
  G4double fPSFCoreZ = 1. * CLHEP::m;

  G4double fClad1RX = -1.;
  G4double fClad1RY = -1.;
  G4double fClad1Z = -1.;

  G4double fClad2RX = -1.;
  G4double fClad2RY = -1.;
  G4double fClad2Z = -1.;

  G4double fClrfiberHalfL = -1.;
  G4double fClrfiberZ = -1.;

  G4double fCoupleRX = -1.;
  G4double fCoupleRY = -1.;
  G4double fCoupleZ = -1.;

  G4double fMirrorRmax = -1.;
  G4double fMirrorZ = 0.1 * CLHEP::mm;
  G4bool fMirrorToggle = true;

  G4String fMPPCShape = "Circle";
  G4double fMPPCHalfL = -1.;
  G4double fMPPCZ = 0.05 * CLHEP::mm;
  G4double fMPPCDist = 0.;
  G4double fMPPCTheta = 0;

  G4double fPSFCoreOrigin = 0.;
  G4double fCoupleOrigin = 0.;
  G4double fMirrorOrigin = 0.;
  G4double fMPPCOriginX = 0.;
  G4double fMPPCOriginZ = 0.;

  G4int fNumOfCladLayers = 0;

  G4double fMirrorPolish = 1.;
  G4double fMirrorReflectivity = 1.;
  G4double fMPPCPolish = 1.;
  G4double fMPPCReflectivity = 0.;
  G4double fExtrusionPolish = 1.;
  G4double fExtrusionReflectivity = 1.;
  G4double fSurfaceRoughness = 1.;
  G4double fXYRatio = 1.;

  G4double fBarLength = 1. * CLHEP::m;
  G4double fBarBase = 9.6 * CLHEP::mm;
  G4double fHoleRadius = 0.9 * CLHEP::mm;
  G4double fHoleLength = -1.;
  G4double fCoatingThickness = 0.25 * CLHEP::mm;
  G4double fCoatingRadius = 1.875 * CLHEP::mm;

  void UpdateGeometryParameters();

  PSFDetectorMessenger *fDetectorMessenger = nullptr;
  G4Cache<PSFPhotonDetSD *> fmppcSD;
};

#endif
